//
//  SubscribeCellNode.m
//  EducationLive
//
//  Created by Apple on 2017/10/19.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "SubscribeCellNode.h"

@interface SubscribeCellNode ()
// UI
@property (nonatomic, strong) ASImageNode *iconNode;
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASTextNode *priceNode;
@property (nonatomic, strong) ASTextNode *timeNode;
@property (nonatomic, strong) ASImageNode *lineNode;

@end

@implementation SubscribeCellNode

- (instancetype)initWithItem
{
    if (self = [super init]) {

        self.backgroundColor = [UIColor whiteColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addIconNode];
        
        [self addTitleNode];
        
        [self addPriceNode];

        [self addTimeNode];

        [self addLineNode];
    }
    return self;
}

- (void)addIconNode
{
    ASImageNode *iconNode = [[ASImageNode alloc]init];
    iconNode.layerBacked = YES;
    iconNode.backgroundColor = [UIColor clearColor];
    iconNode.image = [UIImage imageNamed:@"home_hot_1"];
    [self addSubnode:iconNode];
    _iconNode = iconNode;
}

- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 2;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f] ,NSForegroundColorAttributeName: RGB_255(51, 51, 51)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:@"小学作文 | 如何避免稳坐教学设计方案53例作文" attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

- (void)addPriceNode
{
    ASTextNode *priceNode = [[ASTextNode alloc]init];
    priceNode.placeholderEnabled = YES;
    priceNode.placeholderColor = RGB_255(245, 245, 245);
    priceNode.layerBacked = YES;
    priceNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f] ,NSForegroundColorAttributeName: RGB_255(153 ,153 ,153)};
    priceNode.attributedText = [[NSAttributedString alloc]initWithString:@"¥ 375" attributes:attrs];
    [self addSubnode:priceNode];
    _priceNode = priceNode;
}

- (void)addTimeNode
{
    ASTextNode *timeNode = [[ASTextNode alloc]init];
    timeNode.placeholderEnabled = YES;
    timeNode.placeholderColor = RGB_255(245, 245, 245);
    timeNode.layerBacked = YES;
    timeNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f] ,NSForegroundColorAttributeName: RGB_255(153 ,153 ,153)};
    timeNode.attributedText = [[NSAttributedString alloc]initWithString:@"开播时间:2017.10.18 17:30" attributes:attrs];
    [self addSubnode:timeNode];
    _timeNode = timeNode;
}

- (void)addLineNode
{
    ASImageNode *lineNode = [[ASImageNode alloc]init];
    lineNode.layerBacked = YES;
    lineNode.backgroundColor = RGB_255(230,230,230);
    [self addSubnode:lineNode];
    _lineNode = lineNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, 0.5);
    _titleNode.style.flexShrink = YES;
    _iconNode.style.preferredSize = CGSizeMake(135, 81);
    
    ASStackLayoutSpec *priceLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_priceNode,_timeNode]];
    
    ASStackLayoutSpec *titleLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsStart children:@[_titleNode,priceLayout]];
    titleLayout.style.flexShrink = YES;
    
    ASStackLayoutSpec *iconLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[_iconNode,titleLayout]];
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10) child:iconLayout];
    
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[insetLayout,_lineNode]];
    
    return lineLayout;
}

@end
